/*** 
 * @_______________#########_______________________ 
 * @______________############_____________________ 
 * @______________#############____________________ 
 * @_____________##__###########___________________ 
 * @____________###__######_#####__________________ 
 * @____________###_#######___####_________________ 
 * @___________###__##########_####________________ 
 * @__________####__###########_####_______________ 
 * @________#####___###########__#####_____________ 
 * @_______######___###_########___#####___________ 
 * @_______#####___###___########___######_________ 
 * @______######___###__###########___######_______ 
 * @_____######___####_##############__######______ 
 * @____#######__#####################_#######_____ 
 * @____#######__##############################____ 
 * @___#######__######_#################_#######___ 
 * @___#######__######_######_#########___######___ 
 * @___#######____##__######___######_____######___ 
 * @___#######________######____#####_____#####____ 
 * @____######________#####_____#####_____####_____ 
 * @_____#####________####______#####_____###______ 
 * @______#####______;###________###______#________ 
 * @________##_______####________####______________ 
 * @
 * @Author: ipk518 121206530@qq.com
 * @Date: 2025-03-06 20:00:55
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-09-27 18:39:53
 * @FilePath: /etws-sxk2307/src/packetResolver/lxCmdV1/devControl.cpp
 * @Description: 
 * @你把时间投在专业上，两三年，你就能在圈子里小有名气。四五年，你就能靠这个专业赚钱。过了10年以上，你就能成为这个领域的专家
 * @Copyright (c) 2025 by etws@quyujiang, All Rights Reserved. 
 */


#include "Server.h"
#include "lxCmdV1.h"
#include "gnss.h"

int pz_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    return 0;
}
int nbj_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    return 0;
}
int tr_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    struct cmdDataType cmdHeader;
    uint16_t sendnum = 0xffff;  
    cmdHeader.DeviceType = t_rwDdr;
    cmdHeader.source_DevType = CurDeviceType;
    cmdHeader.source_DevNum = ComponentNO;
    cmdHeader.dst_DevType =  T_R_Sub_Unit_S;
    cmdHeader.dst_DevNum = allDevice;
    cmdHeader.cmdType = 0;
    cmdHeader.cmdChildType = 15;
    cmdHeader.dataLen = cmd->dataLen;
    
    return Server::Instance().separate(s_ddr,sendnum,&cmdHeader,cmdData);
}
int ad_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    struct cmdDataType cmdHeader;
    uint16_t sendnum = 0xffff;  
    cmdHeader.DeviceType = t_rwDdr;
    cmdHeader.source_DevType = CurDeviceType;
    cmdHeader.source_DevNum = ComponentNO;
    cmdHeader.dst_DevType =  AD_Sub_Unit_S;
    cmdHeader.dst_DevNum = allDevice;
    cmdHeader.cmdType = cmd->cmdType;
    cmdHeader.cmdChildType = cmd->cmdChildType;
    cmdHeader.dataLen = cmd->dataLen;
    
    return Server::Instance().separate(s_ddr,sendnum,&cmdHeader,cmdData);
    return 0;
}

int gps_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    struct cmdDataType cmdHeader;
    uint16_t sendnum = 1; 
    int ret = 0; 
 
    cmdHeader.DeviceType = t_controlCmd;
	cmdHeader.source_DevType = CurDeviceType;
	cmdHeader.source_DevNum = ComponentNO;
	cmdHeader.dst_DevType =  leixing_SERVER_Sub_Unit_S;
	cmdHeader.dst_DevNum = 0;
	cmdHeader.cmdType = cmd->cmdType;
	cmdHeader.cmdChildType = cmd->cmdChildType;
	cmdHeader.dataLen = sizeof(struct gnssUpdateRtu);
    gnssModbusRtu::Instance().read();
    {
        Response m_webRes;
        Server::Instance().createSharePtr(&m_webRes);
        // 收集指定设备ID的应答包并处理，设置超时时长为5秒
        std::vector<std::string> device_ids = {"gnss"};
        //调用collector对象的collectResponses方法，传入设备ID向量、处理应答包的回调函数和超时时长（这里设置为5秒）
        m_webRes.collectResponses(device_ids, [](const std::string& response) {
            qlog_i("%s",response.data());
        },CMD_REQUEST_TIMEOUT);

        ret = m_webRes.Result(device_ids[0]);

        Server::Instance().delSharePtr();

        if(ret != 0) {
            gnssModbusRtu::Instance().getData(&Server::Instance().m_sxk.m_sxkInfo.m_gnssUpdateRtu);
            Server::Instance().separate(s_web,1,&cmdHeader,(uint8_t *)&Server::Instance().m_sxk.m_sxkInfo.m_gnssUpdateRtu);
        } else {
            qlog_e("gnss rtu数据读取失败.");
        }
    }
    return 0;
}

int gps_callback_1(struct cmdDataType* cmd,uint8_t * cmdData)
{
    if(cmd->dataLen != 4) {
        qlog_e("gps读寄存器命令需要4个字节.");
        return -1;
    }

    uint16_t regData[85];
    struct cmdDataType cmdHeader;
    uint16_t sendnum = 1; 
    int ret = 0; 
    int readReg = *(uint16_t *)cmdData;
    int regNum = *(uint16_t *)(cmdData+2);
    qlog_i("readReg:%d,regNum:%d",readReg,regNum);
    if(regNum > 85) {
        qlog_e("gps读寄存器命令最多读取85个寄存器.");
        return -1;
    }
    memset(regData,0,sizeof(regData));
    cmdHeader.DeviceType = t_controlCmd;
	cmdHeader.source_DevType = CurDeviceType;
	cmdHeader.source_DevNum = ComponentNO;
	cmdHeader.dst_DevType =  leixing_SERVER_Sub_Unit_S;
	cmdHeader.dst_DevNum = 0;
	cmdHeader.cmdType = cmd->cmdType;
	cmdHeader.cmdChildType = cmd->cmdChildType;
	cmdHeader.dataLen = regNum*2;
    gnssModbusRtu::Instance().read();
    {
        Response m_webRes;
        Server::Instance().createSharePtr(&m_webRes);
        // 收集指定设备ID的应答包并处理，设置超时时长为5秒
        std::vector<std::string> device_ids = {"gnss"};
        //调用collector对象的collectResponses方法，传入设备ID向量、处理应答包的回调函数和超时时长（这里设置为5秒）
        m_webRes.collectResponses(device_ids, [](const std::string& response) {
            qlog_i("%s",response.data());
        },CMD_REQUEST_TIMEOUT);

        ret = m_webRes.Result(device_ids[0]);

        Server::Instance().delSharePtr();

        if(ret != 0) {
            gnssModbusRtu::Instance().getSpecificReg(readReg,regData,regNum);
            Server::Instance().separate(s_web,1,&cmdHeader,(uint8_t *)regData);
        } else {
            qlog_e("gnss rtu数据读取失败.");
        }
    }
    return 0;
}

int gps_callback_2(struct cmdDataType* cmd,uint8_t * cmdData)
{
    return 0;
}



//上报参数
int update_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    return 0;
}
//风扇控制
int wind_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    return 0;
}
//SXK测试参数配置
int ts_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    return 0;
}